{extend name="layout:base" /}

{block name="main"}
<form action="" method="post" class="pt-3">
    <input type="hidden" name="p" value="{:fm_enc(FM_PATH)}">
    <input type="hidden" name="group" value="1">
    {:token_field()}
    <div class="table-responsive">
        <table class="table table-bordered table-hover table-sm {$tableTheme}" id="main-table">
            <thead class="thead-white">
            <tr>
                <th style="width:3%" class="custom-checkbox-header">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="js-select-all-items" onclick="checkbox_toggle()">
                        <label class="custom-control-label" for="js-select-all-items"></label>
                    </div>
                </th>
                <th><?php echo lng('Name') ?></th>
                <th><?php echo lng('Size') ?></th>
                <th><?php echo lng('Modified') ?></th>
                <?php if (!FM_IS_WIN && empty($hide_Cols)): ?>
                <th><?php echo lng('Perms') ?></th>
                <th><?php echo lng('Owner') ?></th>
                <?php endif; ?>
                <th><?php echo lng('Actions') ?></th>
            </tr>
            </thead>
            <?php
            // link to parent folder
            if ($parent !== false) {
                ?>
            <tr>
                <td class="nosort"></td>
                <td class="border-0" data-sort>
                    <a href="{:addon_url('admin/index/index', ['p' => urlencode($parent)])}"><i class="fa fa-chevron-circle-left go-back"></i> ..</a>
                </td>
                <td class="border-0" data-order></td>
                <td class="border-0" data-order></td>
                <td class="border-0"></td>
                <?php if (!FM_IS_WIN && empty($hide_Cols)) { ?>
                <td class="border-0"></td>
                <td class="border-0"></td>
                <?php } ?>
            </tr>
            <?php
            }
            $ii = 3399;

            foreach ($folders as $f) {
                $is_link = is_link($path . '/' . $f);
                $img = $is_link ? 'icon-link_folder' : 'fa fa-folder-o';
                $modif_raw = filemtime($path . '/' . $f);
                $modif = date(FM_DATETIME_FORMAT, $modif_raw);
                $date_sorting = strtotime(date("F d Y H:i:s.", $modif_raw));
                $filesize_raw = "";
                $filesize = lng('Folder');
                $perms = substr(decoct(fileperms($path . '/' . $f)), -4);
                if (function_exists('posix_getpwuid') && function_exists('posix_getgrgid')) {
                    $owner = posix_getpwuid(fileowner($path . '/' . $f));
                    $group = posix_getgrgid(filegroup($path . '/' . $f));
                    if ($owner === false) {
                        $owner = array('name' => '?');
            }
            if ($group === false) {
            $group = array('name' => '?');
            }
            } else {
            $owner = array('name' => '?');
            $group = array('name' => '?');
            }
            ?>
            <tr>
                <?php if (!FM_READONLY): ?>
                <td class="custom-checkbox-td">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="{$ii}" name="file[]" value="{:fm_enc($f)}">
                        <label class="custom-control-label" for="{$ii}"></label>
                    </div>
                </td>
                <?php endif; ?>
                <td data-sort={:fm_convert_win(fm_enc($f))}>
                    <div class="filename"><a href="?p={:urlencode(trim(FM_PATH . '/' . $f, '/'))}"><i class="{$img}"></i> {:fm_convert_win(fm_enc($f))}
                    </a>{$is_link ? (' &rarr; <i>' . readlink($path . '/' . $f) . '</i>') : ''}</div>
                </td>
                <td data-order="a-{:str_pad($filesize_raw, 18, '0', STR_PAD_LEFT)}">{$filesize}</td>
                <td data-order="a-{$date_sorting}">{$modif}</td>
                <?php if (!FM_IS_WIN && !$hide_Cols): ?>
                <td><?php if (!FM_READONLY): ?><a title="Change Permissions" href="{:addon_url('admin/index/chmod', ['p' => FM_PATH, 'chmod' => $f])}">{$perms}</a><?php else: ?>{$perms}<?php endif; ?>
                </td>
                <td>{$owner['name'] . ':' . $group['name']}</td>
                <?php endif; ?>
                <td class="inline-actions">
                    <?php if (!FM_READONLY): ?>
                    <a title="{:lng('Delete')}" href="{:addon_url('admin/index/delfile', ['p' => FM_PATH, 'del' => $f])}" onclick="confirmDailog(event, '1028','{:lng(\'Delete\').\' \'.lng(\'Folder\')}','{:urlencode($f)}', this.href);"> <i class="fa fa-trash-o" aria-hidden="true"></i></a>
                    <a title="{:lng('Rename')}" href="javascript:;" onclick="rename('{:fm_enc(addslashes(FM_PATH))}', '{:fm_enc(addslashes($f))}');"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
                    <a title="{:lng('CopyTo')}..." href="{:addon_url('admin/index/copy', ['p' => '', 'copy' => trim(FM_PATH . '/' . $f, '/')])}"><i class="fa fa-files-o" aria-hidden="true"></i></a>
                    <?php endif; ?>
                    <!--
                    <a title="{:lng('DirectLink')}" href="{:fm_enc(FM_ROOT_URL . (FM_PATH != '' ? '/' . FM_PATH : '') . '/' . $f . '/')}" target="_blank"><i class="fa fa-link" aria-hidden="true"></i></a>
                    -->
                </td>
            </tr>
            <?php
                flush();
                $ii++;
            }
            $ik = 6070;
            foreach ($files as $f) {
                $is_link = is_link($path . '/' . $f);
                $img = $is_link ? 'fa fa-file-text-o' : fm_get_file_icon_class($path . '/' . $f);
                $modif_raw = filemtime($path . '/' . $f);
                $modif = date(FM_DATETIME_FORMAT, $modif_raw);
                $date_sorting = strtotime(date("F d Y H:i:s.", $modif_raw));
                $filesize_raw = fm_get_size($path . '/' . $f);
                $filesize = fm_get_filesize($filesize_raw);
                $filelink = addon_url("admin/index/detail", ["p" => FM_PATH, "f" => $f]);
                $all_files_size += $filesize_raw;
                $perms = substr(decoct(fileperms($path . '/' . $f)), -4);
                if (function_exists('posix_getpwuid') && function_exists('posix_getgrgid')) {
                    $owner = posix_getpwuid(fileowner($path . '/' . $f));
                    $group = posix_getgrgid(filegroup($path . '/' . $f));
                    if ($owner === false) {
                        $owner = array('name' => '?');
            }
            if ($group === false) {
            $group = array('name' => '?');
            }
            } else {
            $owner = array('name' => '?');
            $group = array('name' => '?');
            }
            ?>
            <tr>
                <?php if (!FM_READONLY): ?>
                <td class="custom-checkbox-td">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="{$ik}" name="file[]" value="{:fm_enc($f)}">
                        <label class="custom-control-label" for="{$ik}"></label>
                    </div>
                </td><?php endif; ?>
                <td data-sort={:fm_enc($f)}>
                    <div class="filename">
                        <a <?php
                           if (in_array(strtolower(pathinfo($f, PATHINFO_EXTENSION)), array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'ico', 'svg', 'webp', 'avif'))): ?>
                        <?php $imagePreview = addon_url('admin/index/preview', ['path' => FM_ROOT_PATH.DS.FM_PATH.DS.$f, 'type' => 'image/jpeg']); ?> href="{$filelink}" data-preview-image="{$imagePreview}" title="{:fm_enc($f)}"
                            <?php else: ?> href="{$filelink}" title="{$f}" <?php endif; ?>>
                                <i class="{$img}"></i> {:fm_convert_win(fm_enc($f))}
                            </a>
                             {$is_link ? (' &rarr; <i>' . readlink($path . '/' . $f) . '</i>') : ''}
                    </div>
                </td>
                <td data-order="b-{:str_pad($filesize_raw, 18, '0', STR_PAD_LEFT)}">
                    <span title="{:printf('%s bytes', $filesize_raw)}">{$filesize}</span>
                </td>
                <td data-order="b-{$date_sorting}">{$modif}</td>
                <?php if (!FM_IS_WIN && !$hide_Cols): ?>
                <td><?php if (!FM_READONLY): ?><a title="{:lng('Change Permissions')}" href="{:addon_url('admin/index/chmod', ['p' => FM_PATH, 'chmod' => $f])}">{$perms}</a><?php else: ?>{$perms}<?php endif; ?>
                </td>
                <td>{:fm_enc($owner['name'] . ':' . $group['name'])}</td>
                <?php endif; ?>
                <td class="inline-actions">
                    <?php if (!FM_READONLY): ?>
                    <a title="<?php echo lng('Delete') ?>" href="{:addon_url('admin/index/delfile', ['p' => FM_PATH, 'del' => $f])}" onclick="confirmDailog(event, 1209, '{:lng(\'Delete\')} {:lng(\'File\')}','{:urlencode($f)}', this.href);"> <i class="fa fa-trash-o"></i></a>
                    <a title="<?php echo lng('Rename') ?>" href="javascript:;" onclick="rename('{:fm_enc(addslashes(FM_PATH))}', '{:fm_enc(addslashes($f))}');"><i class="fa fa-pencil-square-o"></i></a>
                    <a title="<?php echo lng('CopyTo') ?>..." href="{:addon_url('admin/index/copy', ['p' => FM_PATH, 'copy' => trim(FM_PATH . '/' . $f, '/')])}"><i class="fa fa-files-o"></i></a>
                    <?php endif; ?>
                    <!--<a title="<?php echo lng('DirectLink') ?>" href="<?php echo fm_enc(FM_ROOT_URL . (FM_PATH != '' ? '/' . FM_PATH : '') . '/' . $f) ?>" target="_blank"><i class="fa fa-link"></i></a>-->
                    <a title="<?php echo lng('Download') ?>" href="{:addon_url('admin/index/download', ['p' => FM_PATH, 'dl' => $f])}" onclick="confirmDailog(event, 1211, '{:lng(\'Download\')}','{:urlencode($f)}', this.href);"><i class="fa fa-download"></i></a>
                </td>
            </tr>
            <?php
                flush();
                $ik++;
            }
            if (empty($folders) && empty($files)) { ?>
            <tfoot>
            <tr><?php if (!FM_READONLY): ?>
                <td></td><?php endif; ?>
                <td colspan="{(!FM_IS_WIN && !$hide_Cols) ? '6' : '4' }"><em>{:lng('Folder is empty')}</em></td>
            </tr>
            </tfoot>
            <?php
            } else { ?>
            <tfoot>
            <tr>
                <td class="gray" colspan="<?php echo (!FM_IS_WIN && !$hide_Cols) ? (FM_READONLY ? '6' :'7') : (FM_READONLY ? '4' : '5') ?>">
                    <?php echo lng('FullSize').': <span class="badge text-bg-light border-radius-0">'.fm_get_filesize($all_files_size).'</span>' ?>
                    <?php echo lng('File').': <span class="badge text-bg-light border-radius-0">'.$num_files.'</span>' ?>
                    <?php echo lng('Folder').': <span class="badge text-bg-light border-radius-0">'.$num_folders.'</span>' ?>
                </td>
            </tr>
            </tfoot>
            <?php } ?>
        </table>
    </div>

    <div class="row footer">
        <?php if (!FM_READONLY): ?>
        <div class="col-xs-12 col-sm-9">
            <ul class="list-inline footer-action">
                <li class="list-inline-item"> <a href="#/select-all" class="btn btn-small btn-outline-primary btn-2" onclick="select_all();return false;"><i class="fa fa-check-square"></i> {:lng('SelectAll')} </a></li>
                <li class="list-inline-item"><a href="#/unselect-all" class="btn btn-small btn-outline-primary btn-2" onclick="unselect_all();return false;"><i class="fa fa-window-close"></i> {:lng('UnSelectAll')} </a></li>
                <li class="list-inline-item"><a href="#/invert-all" class="btn btn-small btn-outline-primary btn-2" onclick="invert_all();return false;"><i class="fa fa-th-list"></i> {:lng('InvertSelection')} </a></li>
                <li class="list-inline-item"><input type="submit" class="hidden" name="delete" id="a-delete" value="Delete" onclick="return confirm('{:lng(\'Delete selected files and folders?\')}')">
                    <a href="javascript:document.getElementById('a-delete').click();" class="btn btn-small btn-outline-danger btn-2"><i class="fa fa-trash"></i> {:lng('Delete')}</a></li>
                <li class="list-inline-item"><input type="submit" class="hidden" name="zip" id="a-zip" value="zip" onclick="return confirm('{:lng(\'Create archive?\')}')">
                    <a href="javascript:document.getElementById('a-zip').click();" class="btn btn-small btn-outline-warning btn-2"><i class="fa fa-file-archive-o"></i> {:lng('Zip')}</a></li>
                <li class="list-inline-item"><input type="submit" class="hidden" name="tar" id="a-tar" value="tar" onclick="return confirm('{:lng(\'Create archive?\')}')">
                    <a href="javascript:document.getElementById('a-tar').click();" class="btn btn-small btn-outline-warning btn-2"><i class="fa fa-file-archive-o"></i> {:lng('Tar')} </a></li>
                <li class="list-inline-item"><input type="submit" class="hidden" name="copy" id="a-copy" value="Copy">
                    <a href="javascript:;" class="js-mass-copy btn btn-small btn-outline-success btn-2"><i class="fa fa-files-o"></i>{:lng('Copy')}/{:lng('Move')} </a></li>
            </ul>
        </div>
        <div class="col-3 d-none d-sm-block">
            <a href="" target="_blank" class="float-right text-muted" style="margin-right: 10px;">{$app_info.title} {$app_info.version}</a>
        </div>
        <?php else: ?>
        <div class="col-12"><a href="" target="_blank" class="float-right text-muted" style="margin-right: 10px;">{$app_info.title} {$app_info.version}</a></div>
        <?php endif; ?>
    </div>
</form>

<script>
    $(document).ready(function () {
        $('.js-mass-copy').on('click', function () {
            $(this).parents('form').attr('action',  "{:addon_url('admin/index/masscopy')}");
            document.getElementById('a-copy').click();
        });
    });
</script>

<style>
    .table-responsive{padding-bottom: 50px;}
    {if $theme == 'light'}
    .footer{
        position: fixed;
        bottom: 0;
        width: 100%;
        background: #fff;
        padding-top: 10px;
    }
    {/if}
</style>
{/block}